<template>
  <div>
    <div class="search-result-container">
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <!-- 文章列表 -->
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <ArticleItem
          v-for="obj in articleList"
          :key="obj.art_id"
          :artobj="obj"
          :isShow="false"
          @click.native="itemClickFn(obj.art_id)"
        ></ArticleItem>
        <!-- 事件修饰符.native -> 给组件内根标签,绑定原生click点击事件 -->
      </van-list>
    </div>
  </div>
</template>

<script>
import { searchResultApi } from "@/api/index";
import ArticleItem from "@/components/ArticleItem.vue";
export default {
  components: {
    ArticleItem,
  },
  data() {
    return {
      page: 1, //页数
      articleList: [], //文章列表
      loading: false, //加载状态
      finished: false, //是否全部加载完成
    };
  },
  async created() {
    const res = await searchResultApi({
      page: this.page,
      q: this.$route.params.kw,
    });
    console.log("搜索结果", res);
    this.articleList = res.data.data.results;
  },

  methods: {
    async onLoad() {
      if (this.articleList.length > 0) {
        this.page++;
        const res = await searchResultApi({
          page: this.page,
          q: this.$route.params.kw,
        });

        if (res.data.data.results === 0) {
          this.finished = true; //没有更多数据
          return;
        }

        console.log("搜索结果", res);
        this.articleList = [...this.articleList, ...res.data.data.results];
        this.loading = false;
      }
    },
    // 文章详情
    itemClickFn(id) {
      this.$router.push({
        path: `/delail?art_id=${id}`,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
  .van-nav-bar {
    background: #1989fa;

    /deep/.van-nav-bar__title {
      color: #fff;
    }
    /deep/.van-nav-bar__text {
      color: #fff;
    }
    /deep/.van-nav-bar__left .van-icon-arrow-left {
      color: #fff;
    }
  }
}
</style>